#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int dx[4] = { 1, -1, 0, 0 };
    int dy[4] = { 0, 0, 1, -1 };
    bool bp[201][201], c = true;
    void solve(vector<vector<char>>& board) {
        for (int i = 0; i < board.size(); i++)
            for (int j = 0; j < board[i].size(); j++)
                if (board[i][j] == 'O' && bp[i][j] == false)
                {
                    c = true;
                    dfs(board, i, j);
                    if (c) change(board, i, j);
                }
    }
    void dfs(vector<vector<char>>& board, int x, int y)
    {
        bp[x][y] = true;

        for (int k = 0; k < 4; k++)
        {
            int a = x + dx[k], b = y + dy[k];
            if (a < 0 || a >= board.size() || b < 0 || b >= board[a].size())
            {
                c = false;
                continue;
            }
            if (board[a][b] == 'O' && bp[a][b] == false) dfs(board, a, b);
        }
    }
    void change(vector<vector<char>>& board, int x, int y)
    {
        board[x][y] = 'X';
        for (int k = 0; k < 4; k++)
        {
            int a = x + dx[k], b = y + dy[k];
            if (a >= 0 && a < board.size() && b >= 0 && b < board[a].size() && board[a][b] == 'O') change(board, a, b);
        }
    }
};

class Solution {
public:
    int dx[4] = { 1, -1, 0, 0 };
    int dy[4] = { 0, 0, 1, -1 };
    bool bp[201][201], c = true;
    void solve(vector<vector<char>>& board) {
        int n = board.size(), m = board[0].size();
        for (int i = 0; i < n; i++)
            if (board[i][0] == 'O' && bp[i][0] == false)
                bfs(board, i, 0, 'O');
        for (int i = 0; i < n; i++)
            if (board[i][m - 1] == 'O' && bp[i][m - 1] == false)
                bfs(board, i, m - 1, 'O');
        for (int j = 0; j < m; j++)
            if (board[0][j] == 'O' && bp[0][j] == false)
                bfs(board, 0, j, 'O');
        for (int j = 0; j < m; j++)
            if (board[n - 1][j] == 'O' && bp[n - 1][j] == false)
                bfs(board, n - 1, j, 'O');
        for (int i = 1; i < n - 1; i++)
            for (int j = 1; j < m - 1; j++)
                if (board[i][j] == 'O' && bp[i][j] == false)
                    bfs(board, i, j, 'X');
    }
    void bfs(vector<vector<char>>& board, int i, int j, char ch) {
        queue<pair<int, int>> q;
        q.push({ i, j });
        bp[i][j] = true;
        board[i][j] = ch;
        while (q.size()) {
            auto [x, y] = q.front();
            q.pop();
            for (int k = 0; k < 4; k++) {
                int a = x + dx[k], b = y + dy[k];
                if (a >= 0 && a < board.size() && b >= 0 && b < board[a].size() && board[a][b] == 'O' && bp[a][b] == false)
                    bp[a][b] = true, board[a][b] = ch, q.push({ a, b });
            }
        }
    }
};